﻿/**  
* mjjOrder.cs
*
* 功 能： N/A
* 类 名： mjjOrder
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013/11/26 9:29:15   N/A    初版
*
*  
*┌──────────────────────────────────┐
*│　 　│
*│　版权所有：YKF,LB　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Db.DAL
{
	/// <summary>
	/// 数据访问类:mjjOrder
	/// </summary>
	public partial class mjjOrder
	{
		public mjjOrder()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("Id", "mjjOrder"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from mjjOrder");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,8)			};
			parameters[0].Value = Id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Model.mjjOrder Model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into mjjOrder(");
            strSql.Append("Enabled,VentilateMjj,CloseMJJ,PowerOff,PowerOn,MoveNo,MoveRight,MoveLeft,Id,OrderId,AreaId,Stop)");
            strSql.Append(" values (");
            strSql.Append("@Enabled,@VentilateMjj,@CloseMJJ,@PowerOff,@PowerOn,@MoveNo,@MoveRight,@MoveLeft,@Id,@OrderId,@AreaId,@Stop)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Enabled", DbType.Int32,8),
					new SQLiteParameter("@VentilateMjj", DbType.Int32,8),
					new SQLiteParameter("@CloseMJJ", DbType.Int32,8),
					new SQLiteParameter("@PowerOff", DbType.Int32,8),
					new SQLiteParameter("@PowerOn", DbType.Int32,8),
					new SQLiteParameter("@MoveNo", DbType.Int32,8),
					new SQLiteParameter("@MoveRight", DbType.Int32,8),
					new SQLiteParameter("@MoveLeft", DbType.Int32,8),
					new SQLiteParameter("@Id", DbType.Int32,8),
					new SQLiteParameter("@OrderId", DbType.Int32,8),
					new SQLiteParameter("@AreaId", DbType.Int32,8),
					new SQLiteParameter("@Stop", DbType.Int32,8)};
            parameters[0].Value = Model.Enabled;
            parameters[1].Value = Model.VentilateMjj;
            parameters[2].Value = Model.CloseMJJ;
            parameters[3].Value = Model.PowerOff;
            parameters[4].Value = Model.PowerOn;
            parameters[5].Value = Model.MoveNo;
            parameters[6].Value = Model.MoveRight;
            parameters[7].Value = Model.MoveLeft;
            parameters[8].Value = Model.Id;
            parameters[9].Value = Model.OrderId;
            parameters[10].Value = Model.AreaId;
            parameters[11].Value = Model.Stop;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.mjjOrder Model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update mjjOrder set ");
            strSql.Append("Enabled=@Enabled,");
            strSql.Append("VentilateMjj=@VentilateMjj,");
            strSql.Append("CloseMJJ=@CloseMJJ,");
            strSql.Append("PowerOff=@PowerOff,");
            strSql.Append("PowerOn=@PowerOn,");
            strSql.Append("MoveNo=@MoveNo,");
            strSql.Append("MoveRight=@MoveRight,");
            strSql.Append("MoveLeft=@MoveLeft,");
            strSql.Append("OrderId=@OrderId,");
            strSql.Append("AreaId=@AreaId,");
            strSql.Append("Stop=@Stop");
            strSql.Append(" where Id=@Id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Enabled", DbType.Int32,8),
					new SQLiteParameter("@VentilateMjj", DbType.Int32,8),
					new SQLiteParameter("@CloseMJJ", DbType.Int32,8),
					new SQLiteParameter("@PowerOff", DbType.Int32,8),
					new SQLiteParameter("@PowerOn", DbType.Int32,8),
					new SQLiteParameter("@MoveNo", DbType.Int32,8),
					new SQLiteParameter("@MoveRight", DbType.Int32,8),
					new SQLiteParameter("@MoveLeft", DbType.Int32,8),
					new SQLiteParameter("@OrderId", DbType.Int32,8),
					new SQLiteParameter("@AreaId", DbType.Int32,8),
					new SQLiteParameter("@Stop", DbType.Int32,8),
					new SQLiteParameter("@Id", DbType.Int32,8)};
            parameters[0].Value = Model.Enabled;
            parameters[1].Value = Model.VentilateMjj;
            parameters[2].Value = Model.CloseMJJ;
            parameters[3].Value = Model.PowerOff;
            parameters[4].Value = Model.PowerOn;
            parameters[5].Value = Model.MoveNo;
            parameters[6].Value = Model.MoveRight;
            parameters[7].Value = Model.MoveLeft;
            parameters[8].Value = Model.OrderId;
            parameters[9].Value = Model.AreaId;
            parameters[10].Value = Model.Stop;
            parameters[11].Value = Model.Id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
            int rows;
            if (Id != -1)
            {
                strSql.Append("delete from mjjOrder ");
                strSql.Append(" where Id=@Id ");
                SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,8)			};
                parameters[0].Value = Id;

                rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            }
            else
            {
                strSql.Append("delete from mjjOrder ");
                SQLiteParameter[] parameters = null;
                rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
 
            }
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from mjjOrder ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.mjjOrder GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select Enabled,VentilateMjj,CloseMJJ,PowerOff,PowerOn,MoveNo,MoveRight,MoveLeft,Id,OrderId,AreaId,Stop from mjjOrder ");
            strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,8)			};
			parameters[0].Value = Id;

			Model.mjjOrder Model=new Model.mjjOrder();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.mjjOrder DataRowToModel(DataRow row)
		{
			Model.mjjOrder Model=new Model.mjjOrder();
			if (row != null)
			{
				if(row["Enabled"]!=null && row["Enabled"].ToString()!="")
				{
					Model.Enabled=int.Parse(row["Enabled"].ToString());
				}
				if(row["VentilateMjj"]!=null && row["VentilateMjj"].ToString()!="")
				{
					Model.VentilateMjj=int.Parse(row["VentilateMjj"].ToString());
				}
				if(row["CloseMJJ"]!=null && row["CloseMJJ"].ToString()!="")
				{
					Model.CloseMJJ=int.Parse(row["CloseMJJ"].ToString());
				}
				if(row["PowerOff"]!=null && row["PowerOff"].ToString()!="")
				{
					Model.PowerOff=int.Parse(row["PowerOff"].ToString());
				}
				if(row["PowerOn"]!=null && row["PowerOn"].ToString()!="")
				{
					Model.PowerOn=int.Parse(row["PowerOn"].ToString());
				}
				if(row["MoveNo"]!=null && row["MoveNo"].ToString()!="")
				{
					Model.MoveNo=int.Parse(row["MoveNo"].ToString());
				}
				if(row["MoveRight"]!=null && row["MoveRight"].ToString()!="")
				{
					Model.MoveRight=int.Parse(row["MoveRight"].ToString());
				}
				if(row["MoveLeft"]!=null && row["MoveLeft"].ToString()!="")
				{
					Model.MoveLeft=int.Parse(row["MoveLeft"].ToString());
				}
				if(row["Id"]!=null && row["Id"].ToString()!="")
				{
					Model.Id=int.Parse(row["Id"].ToString());
				}
				if(row["OrderId"]!=null && row["OrderId"].ToString()!="")
				{
					Model.OrderId=int.Parse(row["OrderId"].ToString());
				}
				if(row["AreaId"]!=null && row["AreaId"].ToString()!="")
				{
					Model.AreaId=int.Parse(row["AreaId"].ToString());
				}
                if (row["Stop"] != null && row["Stop"].ToString() != "")
                {
                    Model.Stop = int.Parse(row["Stop"].ToString());
                }
			}
			return Model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select Enabled,VentilateMjj,CloseMJJ,PowerOff,PowerOn,MoveNo,MoveRight,MoveLeft,Id,OrderId,AreaId,Stop ");
            strSql.Append(" FROM mjjOrder ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return DbHelperSQLite.QueryDataSet(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM mjjOrder ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from mjjOrder T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQLite.QueryDataSet(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "mjjOrder";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

